﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace GFrame.Data
{
    public struct CacheInfo
    {
        public DateTime RecordTime;
        public long ReadCount;
        public object Data;
    }

    /// <summary>
    /// 小型数据缓存类
    /// Jay.Gu
    /// </summary>
    public class MiniCache
    {
        protected Dictionary<string, CacheInfo> _dicCache;

        protected int iMAX = 1000;

        public MiniCache()
        {
            _dicCache = new Dictionary<string, CacheInfo>();
        }

        /// <summary>
        /// 添加到缓存
        /// </summary>
        /// <param name="Key"></param>
        /// <param name="Data"></param>
        public void Add(string Key, object Data)
        {
            CacheInfo ci = new CacheInfo();

            ci.RecordTime = DateTime.Now;
            ci.ReadCount = 0;
            ci.Data = Data;

            _dicCache[Key] = ci;

        }

        /// <summary>
        /// 从缓存获取
        /// </summary>
        /// <param name="Key"></param>
        /// <returns></returns>
        public object Get(string Key)
        {
            if (_dicCache.ContainsKey(Key))
            {
                return _dicCache[Key].Data;
            }

            return null;
        }

        private void DoClear()
        {
            //去除不用的数据

            if (_dicCache.Count > iMAX)
            {
                foreach (string strKey in _dicCache.Keys)
                {
                    if (_dicCache[strKey].ReadCount == 1)
                    {
                        _dicCache.Remove(strKey);
                    }
                }
            }

        }
    }
}
